home *** CD-ROM | disk | FTP | other *** search
/ Sprite 1984 - 1993 / Sprite 1984 - 1993.iso / src / cmds / gdb.new / gdb-3.98 / bfd.mips / aoutf1.h < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-23  |  15.5 KB  |  521 lines

  1. /* A.out "format 1" file handling code
  2.    Copyright (C) 1990-1991 Free Software Foundation, Inc.
  3.    Written by Cygnus Support.
  4.  
  5. This file is part of BFD, the Binary File Descriptor library.
  6.  
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 2 of the License, or
  10. (at your option) any later version.
  11.  
  12. This program is distributed in the hope that it will be useful,
  13. but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. GNU General Public License for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with this program; if not, write to the Free Software
  19. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  20.  
  21. #include <ansidecl.h>
  22. #include <sysdep.h>
  23. struct external_exec;
  24. #include <a.out.sun4.h>
  25. #include "bfd.h"
  26. #include "libaout.h"           
  27. #include "libbfd.h"
  28.  
  29. #include "aout64.h"
  30. #include "stab.gnu.h"
  31. #include "ar.h"
  32.  
  33. /*
  34. The file @code{aoutf1.h} contains the code for BFD's
  35. a.out back end. Control over the generated back end is given by these
  36. two preprocessor names:
  37. @table @code
  38. @item ARCH_SIZE
  39. This value should be either 32 or 64, depending upon the size of an
  40. int in the target format. It changes the sizes of the structs which
  41. perform the memory/disk mapping of structures.
  42.  
  43. The 64 bit backend may only be used if the host compiler supports 64
  44. ints (eg long long with gcc), by defining the name @code{HOST_64_BIT} in @code{bfd.h}.
  45. With this name defined, @emph{all} bfd operations are performed with 64bit
  46. arithmetic, not just those to a 64bit target.
  47.  
  48. @item TARGETNAME
  49. The name put into the target vector.
  50. @item
  51. @end table
  52.  
  53. */
  54.  
  55. void (*bfd_error_trap)();
  56.  
  57. static bfd_target *sunos4_callback ();
  58.  
  59. /*SUPPRESS558*/
  60. /*SUPPRESS529*/
  61.  
  62. bfd_target *
  63. DEFUN(NAME(sunos,object_p), (abfd),
  64.      bfd *abfd)
  65. {
  66.   unsigned char magicbuf[4];    /* Raw bytes of magic number from file */
  67.   unsigned long magic;        /* Swapped magic number */
  68.  
  69.   bfd_error = system_call_error;
  70.  
  71.   if (bfd_read ((PTR)magicbuf, 1 , 4, abfd) !=
  72.       sizeof (magicbuf))
  73.     return 0;
  74.   magic = bfd_h_get_32 (abfd, magicbuf);
  75.  
  76.   if (N_BADMAG (*((struct internal_exec *) &magic))) return 0;
  77.  
  78.   return NAME(aout,some_aout_object_p) (abfd, sunos4_callback);
  79. }
  80.  
  81.   /* Determine the size of a relocation entry, based on the architecture */
  82. static void
  83. DEFUN(choose_reloc_size,(abfd),
  84. bfd *abfd)
  85.   {
  86.     switch (abfd->obj_arch) {
  87.     case bfd_arch_sparc:
  88.     case bfd_arch_a29k:
  89.       obj_reloc_entry_size (abfd) = RELOC_EXT_SIZE;
  90.       break;
  91.     default:
  92.       obj_reloc_entry_size (abfd) = RELOC_STD_SIZE;
  93.       break;
  94.     }
  95.   }
  96.  
  97. /* Set parameters about this a.out file that are machine-dependent.
  98.    This routine is called from some_aout_object_p just before it returns.  */
  99.  
  100. static bfd_target *
  101. sunos4_callback (abfd)
  102.      bfd *abfd;
  103. {
  104.   struct internal_exec *execp = exec_hdr (abfd);
  105.  
  106.   WORK_OUT_FILE_POSITIONS(abfd, execp);  
  107.  
  108.   /* Determine the architecture and machine type of the object file.  */
  109.   switch (N_MACHTYPE (*exec_hdr (abfd))) {
  110.     
  111.   case M_UNKNOWN:
  112.     abfd->obj_arch = bfd_arch_unknown;
  113.     abfd->obj_machine = 0;
  114.     break;
  115.     
  116.   case M_68010:
  117.     abfd->obj_arch = bfd_arch_m68k;
  118.     abfd->obj_machine = 68010;
  119.     break;
  120.     
  121.   case M_68020:
  122.     abfd->obj_arch = bfd_arch_m68k;
  123.     abfd->obj_machine = 68020;
  124.     break;
  125.     
  126.   case M_SPARC:
  127.     abfd->obj_arch = bfd_arch_sparc;
  128.     abfd->obj_machine = 0;
  129.     break;
  130.     
  131.   case M_386:
  132.     abfd->obj_arch = bfd_arch_i386;
  133.     abfd->obj_machine = 0;
  134.     break;
  135.     
  136.   case M_29K:
  137.     abfd->obj_arch = bfd_arch_a29k;
  138.     abfd->obj_machine = 0;
  139.     break;
  140.     
  141.   default:
  142.     abfd->obj_arch = bfd_arch_obscure;
  143.     abfd->obj_machine = 0;
  144.     break;
  145.   }
  146.   
  147.   choose_reloc_size(abfd);
  148.   return abfd->xvec;
  149. }
  150.  
  151.  
  152. /* Write an object file in SunOS format.
  153. Section contents have already been written.  We write the
  154. file header, symbols, and relocation.  */
  155.  
  156. boolean
  157. DEFUN(NAME(aout,sunos4_write_object_contents),(abfd),
  158.       bfd *abfd)
  159.  
  160.   {
  161.     bfd_size_type data_pad = 0;
  162.     struct external_exec exec_bytes;
  163.     struct internal_exec *execp = exec_hdr (abfd);
  164.     
  165.     
  166.     
  167.     execp->a_text = obj_textsec (abfd)->size;
  168.     
  169.     /* Magic number, maestro, please!  */
  170.     switch (bfd_get_architecture(abfd)) {
  171.     case bfd_arch_m68k:
  172.       switch (bfd_get_machine(abfd)) {
  173.       case 68010:
  174.     N_SET_MACHTYPE(*execp, M_68010);
  175.     break;
  176.       default:
  177.       case 68020:
  178.     N_SET_MACHTYPE(*execp, M_68020);
  179.     break;
  180.       }
  181.       break;
  182.     case bfd_arch_sparc:
  183.       N_SET_MACHTYPE(*execp, M_SPARC);
  184.       break;
  185.     case bfd_arch_i386:
  186.       N_SET_MACHTYPE(*execp, M_386);
  187.       break;
  188.     case bfd_arch_a29k:
  189.       N_SET_MACHTYPE(*execp, M_29K);
  190.       break;
  191.     default:
  192.       N_SET_MACHTYPE(*execp, M_UNKNOWN);
  193.     }
  194.     
  195.     choose_reloc_size(abfd);
  196.  
  197.     /* FIXME */
  198.     N_SET_FLAGS (*execp, 0x1);
  199.     
  200.     WRITE_HEADERS(abfd, execp);
  201.  
  202.   return true;
  203. }
  204.  
  205. /* core files */
  206.  
  207. #define CORE_MAGIC 0x080456
  208. #define CORE_NAMELEN 16
  209.  
  210. /* The core structure is taken from the Sun documentation.
  211. Unfortunately, they don't document the FPA structure, or at least I
  212. can't find it easily.  Fortunately the core header contains its own
  213. length.  So this shouldn't cause problems, except for c_ucode, which
  214. so far we don't use but is easy to find with a little arithmetic. */
  215.  
  216. /* But the reg structure can be gotten from the SPARC processor handbook.
  217. This really should be in a GNU include file though so that gdb can use
  218. the same info. */
  219. struct regs {
  220.   int r_psr;
  221.   int r_pc;
  222.   int r_npc;
  223.   int r_y;
  224.   int r_g1;
  225.   int r_g2;
  226.   int r_g3;
  227.   int r_g4;
  228.   int r_g5;
  229.   int r_g6;
  230.   int r_g7;
  231.   int r_o0;
  232.   int r_o1;
  233.   int r_o2;
  234.   int r_o3;
  235.   int r_o4;
  236.   int r_o5;
  237.   int r_o6;
  238.   int r_o7;
  239. };
  240.  
  241. /* Taken from Sun documentation: */
  242.  
  243. /* FIXME:  It's worse than we expect.  This struct contains TWO substructs
  244. neither of whose size we know, WITH STUFF IN BETWEEN THEM!  We can't
  245. even portably access the stuff in between!  */
  246.  
  247. struct core {
  248.   int c_magic;            /* Corefile magic number */
  249.   int c_len;            /* Sizeof (struct core) */
  250.   struct regs c_regs;        /* General purpose registers -- MACHDEP SIZE */
  251.   struct internal_exec c_aouthdr;    /* A.out header */
  252.   int c_signo;            /* Killing signal, if any */
  253.   int c_tsize;            /* Text size (bytes) */
  254.   int c_dsize;            /* Data size (bytes) */
  255.   int c_ssize;            /* Stack size (bytes) */
  256.   char c_cmdname[CORE_NAMELEN + 1]; /* Command name */
  257.   double fp_stuff[1];            /* external FPU state (size unknown by us) */
  258.   /* The type "double" is critical here, for alignment.
  259.     SunOS declares a struct here, but the struct's alignment
  260.       is double since it contains doubles.  */
  261.   int c_ucode;            /* Exception no. from u_code */
  262.   /* (this member is not accessible by name since we don't
  263.     portably know the size of fp_stuff.) */
  264. };
  265.  
  266. /* Supposedly the user stack grows downward from the bottom of kernel memory.
  267. Presuming that this remains true, this definition will work. */
  268. #define USRSTACK (-(128*1024*1024))
  269.  
  270. PROTO (static void, swapcore, (bfd *abfd, struct core *core));
  271.  
  272. /* need this cast b/c ptr is really void * */
  273. #define core_hdr(bfd) (((struct suncordata *) (bfd->tdata))->hdr)
  274. #define core_datasec(bfd) (((struct suncordata *) ((bfd)->tdata))->data_section)
  275. #define core_stacksec(bfd) (((struct suncordata*)((bfd)->tdata))->stack_section)
  276. #define core_regsec(bfd) (((struct suncordata *) ((bfd)->tdata))->reg_section)
  277. #define core_reg2sec(bfd) (((struct suncordata *) ((bfd)->tdata))->reg2_section)
  278.  
  279. /* These are stored in the bfd's tdata */
  280. struct suncordata {
  281. struct core *hdr;             /* core file header */
  282. asection *data_section;
  283. asection *stack_section;
  284. asection *reg_section;
  285. asection *reg2_section;
  286. };
  287.  
  288. static bfd_target *
  289. DEFUN(sunos4_core_file_p,(abfd),
  290.       bfd *abfd)
  291. {
  292.   unsigned char longbuf[4];    /* Raw bytes of various header fields */
  293.   int core_size;
  294.   int core_mag;
  295.   struct core *core;
  296.   char *rawptr;
  297.   
  298.   bfd_error = system_call_error;
  299.   
  300.   if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
  301.       sizeof (longbuf))
  302.     return 0;
  303.   core_mag = bfd_h_get_32 (abfd, longbuf);
  304.  
  305.   if (core_mag != CORE_MAGIC) return 0;
  306.  
  307.   /* SunOS core headers can vary in length; second word is size; */
  308.   if (bfd_read ((PTR)longbuf, 1, sizeof (longbuf), abfd) !=
  309.       sizeof (longbuf))
  310.     return 0;
  311.   core_size = bfd_h_get_32 (abfd, longbuf);
  312.   /* Sanity check */
  313.   if (core_size > 20000)
  314.     return 0;
  315.  
  316.   if (bfd_seek (abfd, 0L, false) < 0) return 0;
  317.  
  318.   rawptr = bfd_zalloc (abfd, core_size + sizeof (struct suncordata));
  319.   if (rawptr == NULL) {
  320.     bfd_error = no_memory;
  321.     return 0;
  322.   }
  323.  
  324.   core = (struct core *) (rawptr + sizeof (struct suncordata));
  325.  
  326.   if ((bfd_read ((PTR) core, 1, core_size, abfd)) != core_size) {
  327.     bfd_error = system_call_error;
  328.     bfd_release (abfd, rawptr);
  329.     return 0;
  330.   }
  331.  
  332.   swapcore (abfd, core);
  333.   set_tdata (abfd, ((struct suncordata *) rawptr));
  334.   core_hdr (abfd) = core;
  335.  
  336.   /* create the sections.  This is raunchy, but bfd_close wants to reclaim
  337.      them */
  338.   core_stacksec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
  339.   if (core_stacksec (abfd) == NULL) {
  340.   loser:
  341.     bfd_error = no_memory;
  342.     bfd_release (abfd, rawptr);
  343.     return 0;
  344.   }
  345.   core_datasec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
  346.   if (core_datasec (abfd) == NULL) {
  347.   loser1:
  348.     bfd_release (abfd, core_stacksec (abfd));
  349.     goto loser;
  350.   }
  351.   core_regsec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
  352.   if (core_regsec (abfd) == NULL) {
  353.   loser2:
  354.     bfd_release (abfd, core_datasec (abfd));
  355.     goto loser1;
  356.   }
  357.   core_reg2sec (abfd) = (asection *) bfd_zalloc (abfd, sizeof (asection));
  358.   if (core_reg2sec (abfd) == NULL) {
  359.     bfd_release (abfd, core_regsec (abfd));
  360.     goto loser2;
  361.   }
  362.  
  363.   core_stacksec (abfd)->name = ".stack";
  364.   core_datasec (abfd)->name = ".data";
  365.   core_regsec (abfd)->name = ".reg";
  366.   core_reg2sec (abfd)->name = ".reg2";
  367.  
  368.   core_stacksec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
  369.   core_datasec (abfd)->flags = SEC_ALLOC + SEC_LOAD;
  370.   core_regsec (abfd)->flags = SEC_ALLOC;
  371.   core_reg2sec (abfd)->flags = SEC_ALLOC;
  372.  
  373.   core_stacksec (abfd)->size = core->c_ssize;
  374.   core_datasec (abfd)->size = core->c_dsize;
  375.   core_regsec (abfd)->size = (sizeof core->c_regs);
  376.   /* Float regs take up end of struct, except c_ucode.  */
  377.   core_reg2sec (abfd)->size = core_size - (sizeof core->c_ucode) -
  378.     (file_ptr)(((struct core *)0)->fp_stuff);
  379.  
  380.   core_stacksec (abfd)->vma = (USRSTACK - core->c_ssize);
  381.   core_datasec (abfd)->vma = N_DATADDR(core->c_aouthdr);
  382.   core_regsec (abfd)->vma = -1;
  383.   core_reg2sec (abfd)->vma = -1;
  384.  
  385.   core_stacksec (abfd)->filepos = core->c_len + core->c_dsize;
  386.   core_datasec (abfd)->filepos = core->c_len;
  387.   /* In file header: */
  388.   core_regsec (abfd)->filepos = (file_ptr)(&((struct core *)0)->c_regs);
  389.   core_reg2sec (abfd)->filepos = (file_ptr)(((struct core *)0)->fp_stuff);
  390.  
  391.   /* Align to word at least */
  392.   core_stacksec (abfd)->alignment_power = 2;
  393.   core_datasec (abfd)->alignment_power = 2;
  394.   core_regsec (abfd)->alignment_power = 2;
  395.   core_reg2sec (abfd)->alignment_power = 2;
  396.  
  397.   abfd->sections = core_stacksec (abfd);
  398.   core_stacksec (abfd)->next = core_datasec (abfd);
  399.   core_datasec (abfd)->next = core_regsec (abfd);
  400.   core_regsec (abfd)->next = core_reg2sec (abfd);
  401.  
  402.   abfd->section_count = 4;
  403.  
  404.   return abfd->xvec;
  405. }
  406.  
  407. static char *sunos4_core_file_failing_command (abfd)
  408. bfd *abfd;
  409.   {
  410.   return core_hdr (abfd)->c_cmdname;
  411. }
  412.  
  413. static int
  414. DEFUN(sunos4_core_file_failing_signal,(abfd),
  415.       bfd *abfd)
  416. {
  417.   return core_hdr (abfd)->c_signo;
  418. }
  419.  
  420. static boolean
  421. DEFUN(sunos4_core_file_matches_executable_p, (core_bfd, exec_bfd),
  422.       bfd *core_bfd AND
  423.       bfd *exec_bfd)
  424. {
  425.   if (core_bfd->xvec != exec_bfd->xvec) {
  426.     bfd_error = system_call_error;
  427.     return false;
  428.   }
  429.  
  430.   return (bcmp ((char *)&core_hdr (core_bfd), (char*) &exec_hdr (exec_bfd),
  431.         sizeof (struct internal_exec)) == 0) ? true : false;
  432. }
  433.  
  434. /* byte-swap core structure */
  435. /* FIXME, this needs more work to swap IN a core struct from raw bytes */
  436. static void
  437. DEFUN(swapcore,(abfd, core),
  438.       bfd *abfd AND
  439.       struct core *core)
  440. {
  441.   struct external_exec exec_bytes;
  442.   
  443.   core->c_magic = bfd_h_get_32 (abfd, (unsigned char *)&core->c_magic);
  444.   core->c_len   = bfd_h_get_32 (abfd, (unsigned char *)&core->c_len  );
  445.   /* Leave integer registers in target byte order.  */
  446.   bcopy ((char *)&(core->c_aouthdr), (char *)&exec_bytes, EXEC_BYTES_SIZE);
  447.   NAME(aout,swap_exec_header_in)(abfd, &exec_bytes, &core->c_aouthdr);
  448.   core->c_signo = bfd_h_get_32 (abfd, (unsigned char *)&core->c_signo);
  449.   core->c_tsize = bfd_h_get_32 (abfd, (unsigned char *)&core->c_tsize);
  450.   core->c_dsize = bfd_h_get_32 (abfd, (unsigned char *)&core->c_dsize);
  451.   core->c_ssize = bfd_h_get_32 (abfd, (unsigned char *)&core->c_ssize);
  452.   /* Leave FP registers in target byte order.  */
  453.   /* Leave "c_ucode" unswapped for now, since we can't find it easily.  */
  454. }
  455.  
  456. /* We use BFD generic archive files.  */
  457. #define    aout_32_openr_next_archived_file    bfd_generic_openr_next_archived_file
  458. #define    aout_32_generic_stat_arch_elt        bfd_generic_stat_arch_elt
  459. #define    aout_32_slurp_armap            bfd_slurp_bsd_armap
  460. #define    aout_32_slurp_extended_name_table    bfd_true
  461. #define    aout_32_write_armap            bsd_write_armap
  462. #define    aout_32_truncate_arname            bfd_bsd_truncate_arname
  463. #define aout_32_machine_type             sunos_machine_type
  464.  
  465. #define    aout_32_core_file_failing_command     sunos4_core_file_failing_command
  466. #define    aout_32_core_file_failing_signal    sunos4_core_file_failing_signal
  467. #define    aout_32_core_file_matches_executable_p    sunos4_core_file_matches_executable_p
  468.  
  469.  
  470. #define    aout_64_openr_next_archived_file    bfd_generic_openr_next_archived_file
  471. #define    aout_64_generic_stat_arch_elt        bfd_generic_stat_arch_elt
  472. #define    aout_64_slurp_armap            bfd_slurp_bsd_armap
  473. #define    aout_64_slurp_extended_name_table    bfd_true
  474. #define    aout_64_write_armap            bsd_write_armap
  475. #define    aout_64_truncate_arname            bfd_bsd_truncate_arname
  476. #define aout_64_machine_type             sunos_machine_type
  477.  
  478. #define    aout_64_core_file_failing_command     sunos4_core_file_failing_command
  479. #define    aout_64_core_file_failing_signal    sunos4_core_file_failing_signal
  480. #define    aout_64_core_file_matches_executable_p    sunos4_core_file_matches_executable_p
  481.  
  482. #define aout_64_bfd_debug_info_start        bfd_void
  483. #define aout_64_bfd_debug_info_end        bfd_void
  484. #define aout_64_bfd_debug_info_accumulate    bfd_void
  485.  
  486. #define aout_32_bfd_debug_info_start        bfd_void
  487. #define aout_32_bfd_debug_info_end        bfd_void
  488. #define aout_32_bfd_debug_info_accumulate    (PROTO(void,(*),(bfd*, struct sec *))) bfd_void
  489.  
  490.  
  491.  
  492. /* We implement these routines ourselves, rather than using the generic
  493. a.out versions.  */
  494. #define    aout_write_object_contents    sunos4_write_object_contents
  495.  
  496. bfd_target VECNAME =
  497.   {
  498.     TARGETNAME,
  499.     bfd_target_aout_flavour_enum,
  500.     true,            /* target byte order */
  501.     true,            /* target headers byte order */
  502.     (HAS_RELOC | EXEC_P |    /* object flags */
  503.      HAS_LINENO | HAS_DEBUG |
  504.      HAS_SYMS | HAS_LOCALS | DYNAMIC | WP_TEXT | D_PAGED),
  505.     (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC), /* section flags */
  506.     ' ',                           /* ar_pad_char */
  507.     16,                               /* ar_max_namelen */
  508.     3,                               /* minimum alignment power */
  509.     _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* data */
  510.     _do_getb64, _do_putb64, _do_getb32, _do_putb32, _do_getb16, _do_putb16, /* hdrs */
  511.     
  512.       {_bfd_dummy_target, NAME(sunos,object_p),
  513.        bfd_generic_archive_p, sunos4_core_file_p},
  514.       {bfd_false, NAME(aout,mkobject),
  515.        _bfd_generic_mkarchive, bfd_false},
  516.       {bfd_false, NAME(aout,sunos4_write_object_contents), /* bfd_write_contents */
  517.        _bfd_write_archive_contents, bfd_false},
  518.     
  519.     JUMP_TABLE(JNAME(aout))
  520.     };
  521.